package com.zrq.mytopbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 自定义topbar
 */

public class Topbar extends RelativeLayout {

    //控件
    private Button leftButton, rightButton;
    private TextView tvTitle;

    //左边Button的属性
    private int leftTextColor;
    private Drawable leftBackground;
    private String leftText;

    //右边Button的属性
    private int rightTextColor;
    private Drawable rightBackground;
    private String rightText;

    //title的属性
    private float titleTextSize;
    private int titleTextColor;
    private String title;

    //三个控件在ViewGroup中的布局属性
    private LayoutParams leftParams,rightParams,titleParams;



    // 1、topbar的接口
    public interface topbarClickListener{

        public void leftClick();
        public void rightClick();
    }

    // 2、listener变量用来映射调用者传进来的接口
    private topbarClickListener listener;

    // 3、暴露给外界去调用这个接口的方法
    public void setOnTopbarClickListener(topbarClickListener listener){
        this.listener=listener;
    }

    //构造方法
    public Topbar(final Context context, AttributeSet attrs) {
        super(context, attrs);

        //得到属性集合
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar);

        //从 TypedArray 中取出左边 Button 的自定义属性值
        leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
        leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
        leftText = ta.getString(R.styleable.Topbar_leftText);

        //从 TypedArray 中取出右边 Button 的自定义属性值
        rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);
        rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
        rightText = ta.getString(R.styleable.Topbar_rightText);

        //从 TypedArray 中取出右边 title 的自定义属性值
        titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
        titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
        title = ta.getString(R.styleable.Topbar_title);

        //属性集合使用完毕记得回收
        ta.recycle();

        //创建控件
        leftButton=new Button(context);
        rightButton=new Button(context);
        tvTitle=new TextView(context);

        //为控件设置上自定义属性
        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(leftBackground);
        leftButton.setText(leftText);

        rightButton.setTextColor(rightTextColor);
        rightButton.setBackground(rightBackground);
        rightButton.setText(rightText);

        tvTitle.setText(title);
        tvTitle.setTextColor(titleTextColor);
        tvTitle.setTextSize(titleTextSize);
        tvTitle.setGravity(Gravity.CENTER);

        //设置 viewGroup 的背景
        setBackgroundColor(0xFFF59563);

        //定义 leftButton 在ViewGroup中布局的形式：宽高均为WRAP_CONTENT，并且左对齐
        leftParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);

        addView(leftButton,leftParams);

        //定义 rightButton 在ViewGroup中布局的形式：宽高均为WRAP_CONTENT，并且左对齐
        rightParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);

        addView(rightButton,rightParams);

        //定义 tvTitle 在ViewGroup中布局的形式：宽度为WRAP_CONTENT，高度MATCH_PARENT，并且居中对齐
        titleParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);

        addView(tvTitle,titleParams);

        //为左边 button 设置点击事件
        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.leftClick();
            }
        });

        //为右边 button 设置点击事件
        rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.rightClick();
            }
        });

    }

    //控制左边的Button是否显示
    public void setLeftIsvisable(boolean flag){
        if(flag){
            leftButton.setVisibility(VISIBLE);
        }else{
            leftButton.setVisibility(GONE);
        }
    }
}
